/**
 * Copyright (c) 2008, Julien PORTALIER
 * http://featherphp.googlecode.com/
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 */

Dialog.Window = new Class(
{
	Extends: Dialog.Base,
	
	initialize: function(options)
	{
		this.setOptions({
			'className': 'dialog window',
			'titlebar':  true,
			'title':     ''
		});
		this.parent(options);
		
		// border
		var border = new Element('div', {'class': 'border'});
		this.dialog.grab(border, 'top');
	},
	
	_setPosition: function()
	{
		// dialog's size
		this.dialog.setStyles({'visibility': 'hidden', 'display': 'block'});
		var size = this.dialog.getSize();
		this.dialog.setStyles({'visibility': 'visible', 'display': 'none'});
		
		// available space
		var space  = window.getSize();
		var scroll = window.getScroll();
		
		var left  = parseInt((space.x - size.x) / 2, 10) + scroll.x;
		var top   = parseInt((space.y - size.y) / 2, 10) + scroll.y;
		this.parent(left, top);
	}
});

Dialog.Buttons = new Class(
{
	Extends: Dialog.Window,
	
	initialize: function(content, buttons, options)
	{
		this.parent(options);
		this.setContent('text', content);
		this._setButtons(buttons);
		this.display();
	},
	
	_setButtons: function(buttons)
	{
		this.buttons = new Element('div', {'class': 'buttons'});
		for(var i=0; i<buttons.length; i++)
		{
			this.buttons.appendChild(buttons[i]);
		}
		this.dialog.appendChild(this.buttons);
	}
});

Dialog.Confirm = new Class(
{
	Extends: Dialog.Buttons,
	
	initialize: function(content, options)
	{
		this.setOptions({
			'className': 'dialog window confirm',
			'cancel':    'Cancel',
			'confirm':   'OK',
			'onCancel':  $empty,
			'onConfirm': $empty
		});
		
		// buttons
		var buttons = [
			new Element('button', {
				'text':  this.options.cancel,
				'class': 'cancel',
				'events': {
					'click': this.onCancel.bind(this)
				}
			}),
			new Element('button', {
				'text':  this.options.confirm,
				'class': 'confirm',
				'events': {
					'click': this.onConfirm.bind(this)
				}
			})
		];
		this.parent(content, buttons, options);
	},
	
	onCancel: function()
	{
		this.destroy();
		this.options.onCancel();
	},
	
	onConfirm: function()
	{
		this.destroy();
		this.options.onConfirm();
	}
});

Dialog.Alert = new Class(
{
	Extends: Dialog.Buttons,
	
	initialize: function(content, options)
	{
		this.setOptions({
			'className': 'dialog window alert',
			'close':     'OK',
			'onClose':   $empty
		});
		
		// buttons
		buttons = [
			new Element('button', {
				'text':  this.options.close,
				'class': 'close',
				'events': {
					'click': this.onClose.bind(this)
				}
			})
		];
		this.parent(content, buttons, options);
	},
	
	onClose: function()
	{
		this.destroy();
		this.options.onClose();
	}
});
